<?php

namespace Kunfeng\Dify\Client\Impl;

use GuzzleHttp\Client;
use Kunfeng\Dify\Client\DifyClientInterface;
use Kunfeng\Dify\Client\DifyChatClientInterface;
use Kunfeng\Dify\Client\DifyCompletionClientInterface;
use Kunfeng\Dify\Client\DifyWorkflowClientInterface;
use Kunfeng\Dify\Client\DifyDatasetsClientInterface;
use Kunfeng\Dify\Client\DifyChatflowClientInterface;
use Kunfeng\Dify\Client\RetryHandler;
use Kunfeng\Dify\Exception\DifyApiException;
use Kunfeng\Dify\Model\DifyConfig;

/**
 * Dify 完整客户端实现
 */
class DifyClient implements DifyClientInterface
{
    private $chatClient;
    private $completionClient;
    private $workflowClient;
    private $datasetsClient;
    private $chatflowClient;

    public function __construct(
        string $baseUrl,
        string $apiKey,
        $httpClient = null,
        $retryHandler = null
    ) {
        $this->chatClient = new DifyChatClient($baseUrl, $apiKey, $httpClient, $retryHandler);
        $this->completionClient = new DifyCompletionClient($baseUrl, $apiKey, $httpClient, $retryHandler);
        $this->workflowClient = new DifyWorkflowClient($baseUrl, $apiKey, $httpClient, $retryHandler);
        $this->datasetsClient = new DifyDatasetsClient($baseUrl, $apiKey, $httpClient, $retryHandler);
        $this->chatflowClient = new DifyChatflowClient($baseUrl, $apiKey, $httpClient, $retryHandler);
    }

    /**
     * 从配置创建客户端
     */
    public static function fromConfig(DifyConfig $config): self
    {
        $httpClient = $config->getHttpOptions() 
            ? new Client($config->getHttpOptions())
            : null;

        return new self($config->getBaseUrl(), $config->getApiKey(), $httpClient);
    }

    /**
     * 获取对话型应用客户端
     */
    public function getChatClient(): DifyChatClientInterface
    {
        return $this->chatClient;
    }

    /**
     * 获取文本生成型应用客户端
     */
    public function getCompletionClient(): DifyCompletionClientInterface
    {
        return $this->completionClient;
    }

    /**
     * 获取工作流应用客户端
     */
    public function getWorkflowClient(): DifyWorkflowClientInterface
    {
        return $this->workflowClient;
    }

    /**
     * 获取知识库管理客户端
     */
    public function getDatasetsClient(): DifyDatasetsClientInterface
    {
        return $this->datasetsClient;
    }

    /**
     * 获取工作流编排对话型应用客户端
     */
    public function getChatflowClient(): DifyChatflowClientInterface
    {
        return $this->chatflowClient;
    }

    // 代理方法到各个客户端
    public function uploadFile($fileOrRequest, $filePathOrStream, ?string $user = null): \Kunfeng\Dify\Model\File\FileUploadResponse
    {
        return $this->chatClient->uploadFile($fileOrRequest, $filePathOrStream, $user);
    }

    public function getAppInfo(): \Kunfeng\Dify\Model\Chat\AppInfoResponse
    {
        return $this->chatClient->getAppInfo();
    }

    public function getAppParameters(): \Kunfeng\Dify\Model\Chat\AppParametersResponse
    {
        return $this->chatClient->getAppParameters();
    }

    public function getAppWebAppSettings(): \Kunfeng\Dify\Model\Chat\AppWebAppSettingResponse
    {
        return $this->chatClient->getAppWebAppSettings();
    }

    public function previewFile(string $fileId, bool $asAttachment = false): \Kunfeng\Dify\Model\File\FilePreviewResponse
    {
        return $this->chatClient->previewFile($fileId, $asAttachment);
    }

    public function close(): void
    {
        $this->chatClient->close();
        $this->completionClient->close();
        $this->workflowClient->close();
        $this->datasetsClient->close();
        $this->chatflowClient->close();
    }
}
